import Vue from 'vue'
import VueRouter from 'vue-router'
import HomeView from '../views/HomeView.vue'
import HomeMain from '../components/HomeView/HomeMain.vue'
import HomeRecommend from '../components/HomeView/HomeRecommend.vue'
import HomeSelf from '../components/HomeView/HomeSelf.vue'
import Login from '../components/Login.vue'
import Register from '../components/Register.vue'
import HomeInfo from "../components/HomeView/HomeInfo.vue";
import HomeFollow from "../components/HomeView/HomeFollow.vue";
import SearchSinger from "../components/search/SearchSinger.vue";
import SearchSong from "../components/search/SearchSong.vue";
import SongDetail from "../components/folder/SongDetail.vue"
import FMSelf from "../components/HomeView/FMself.vue"
import SongLove from "../components/SongList/SongLove.vue"
import listcollection from "../components/SongList/listcollection.vue"
import ListLate from "../components/SongList/ListLate.vue"

Vue.use(VueRouter)

//获取原型对象上的push函数
const originalPush = VueRouter.prototype.push
//修改原型对象中的push方法
VueRouter.prototype.push = function push(location) {
  return originalPush.call(this, location).catch(err => err)
}

const routes = [
  {
    path: '/',
    redirect: '/home',
  },
  {
    path: '/home',
    name: 'home',
    component: HomeView,
    redirect:'/homemain',
    children:[
      {
        path: '/homemain',
        component: HomeMain,
      },
      {
        path: '/homerecommend',
        component: HomeRecommend,
      },
      {
        path: '/homeself',
        component: HomeSelf,
      },
      {
        path: "/homeinfo",
        component: HomeInfo,
      },
      {
        path: '/homefollow',
        component: HomeFollow,
      },{
        path: '/searchsinger',
        component: SearchSinger
      },{
        path: '/searchsong',
        component: SearchSong
      },{
        path: '/songdetail',
        component: SongDetail
      },{
        path: '/fmself',
        component: FMSelf
      },{
        path: '/songlove',
        component: SongLove
      },{
        path: '/listcollection',
        component: listcollection
      },{
        path: '/listlate',
        component: ListLate
      }
    ]
  },
  {
    path: '/login',
    name: 'login',
    component: Login,
  },
  {
    path: '/register',
    name: 'register',
    component: Register,
  },
]

const router = new VueRouter({
  routes
})

/** 解决跳转重复路由报错问题 */ 
const routerPush = router.push;
router.push = path => {
  // 判断下当前路由是否就是要跳转的路由
  if (router.currentRoute.fullPath.includes(path)) return;
  return routerPush.call(router, path);
}

export default router
